import {
  get,post
} from "../../untils/db"

Page({
  data: {
    id: 0,
    list: [],
    dialogShow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    addScore: 10,
    menu: [],
    taskCategoryId: 0,
    value:false,
    downScore:0,
    taskType:"",
    isVerify:""
  },
  async onLoad(e) {
    let token = wx.getStorageSync('login')
    let res = await get("/api/task-category/findAll", {}, token)
    this.setData({
      menu: res.data.data
    },()=>{
      console.log(this.data.menu);
    })

    this.setData({
      taskCategoryId:this.data.menu[0].id
    })

    this.setData({
      list:this.data.menu[0].data
    })

    this.setData({
      id:this.data.menu[0].id
    })
    console.log(e, 555);
  },





  down() {
    if(this.data.addScore<=0){
      this.setData({
      addScore: 0
    })
    wx.showToast({
      title: '已经最低分了',
      icon:"none"
    })
    }else{
      this.setData({
      addScore: this.data.addScore - 1
    })
    }
    
  },
  up() {
    this.setData({
      addScore: this.data.addScore + 1
    })
  },

  ups() {
    console.log(1);
    this.setData({
      downScore: this.data.downScore + 1
    })
  },
  downs() {
    if(this.data.downScore<=0){
       this.setData({
      downScore: 0
    })
    wx.showToast({
      title: '已经最低分了',
      icon:"none"
    })
    }else{
       this.setData({
      downScore: this.data.downScore - 1
    })
    }
   
  },


  ji(e) {
    console.log(e.target.id);
    this.setData({
      id: e.target.id
    }, () => {
      console.log(this.data.id);
    })
  },
  hyu() {
    this.setData({
      dialogShow: true
    })
  },
  tapDialogButton(e) {
    console.log(e.detail);
    if (e.detail.index == 1) {
      this.remove()
      wx.reLaunch({
        url: '../../index/index',
      })
    }
    this.setData({
      dialogShow: false
    })
  },
  ku() {
    wx.navigateTo({
      url: '../guneirong/gu',
    })
  },
  ziyi() {
    wx.navigateTo({
      url: '../guneirong/gu',
    })
  },
  yuan() {
    wx.navigateTo({
      url: '../renyuan/ren',
    })
  },
  time() {
    wx.navigateTo({
      url: '../time/time',
    })
  },
  youxiao() {
    wx.navigateTo({
      url: '../timeover/timeover',
    })
  },

  look() {
    console.log(this.data.taskType);
    console.log(this.data.isVerify);
    console.log(this.data.addScore);
    console.log(this.data.downScore);
    
    

    wx.navigateTo({
      url: '../yulan/yulan?taskType='+this.data.taskType+'&isVerify='+this.data.isVerify+'&addScore='+this.data.addScore+'&downScore='+this.data.downScore,
    })
  },
  async release(){

    let release1=wx.getStorageSync('release1')
    let release2=wx.getStorageSync('release2')
    let week=wx.getStorageSync('week')
    let Date=wx.getStorageSync('Date')
    let token=wx.getStorageSync('login')

  let obj={
    taskCategoryId:this.data.taskCategoryId,
    taskSmallCategoryId:this.data.id,
    taskLibId:release1.taskLibId,
    name:release1.name,
    content:release1.content,
    taskMembers:release2,
    publishTime:week.publishTime,
    validStartTime:Date.validStartTime,
    validStartStr:Date.validStartStr,
    validEndTime:Date.validEndTime,
    validEndStr:Date.validEndStr,
    reduplicate:week.reduplicate,
    addScore:this.data.addScore,
    isVerify:this.data.isVerify,
    taskType:this.data.taskType,
    downScore:-this.data.downScore
  }
  
  let arr=[]
  for(const key in obj){
  arr.push(obj[key])
  console.log(obj[key]);
  }
  console.log(arr);
  console.log(arr.indexOf(""));
  console.log(arr.indexOf(undefined));
  if(arr.indexOf(undefined)==-1&&arr.indexOf("")==-1){
    // let res=await post("/api/publish-task/add",obj,token)
    // console.log(res);
    
    this.remove()
    wx.reLaunch({
      url: '../../index/index',
    })

    wx.showToast({
      title: '发布成功',
    })
  }else{
  wx.showToast({
    title: '请填写完整',
    icon:"error"
  })
  }
  

 

  },

  huan(e) {
    console.log(e.target.id);
    this.setData({
      taskCategoryId: e.target.id
    })
  },
  change(e) {
    console.log(e.detail.value);
    this.setData({
      taskType:e.detail.value
    })
    if(e.detail.value==0){
      this.setData({
        value:true
      })
    }else{
      this.setData({
        value:false
      })
    }
  },

  choose(e) {
    console.log(e.detail.value);
      this.setData({
        isVerify:e.detail.value
      })
  },

  remove(){
    wx.removeStorageSync('release1')
    wx.removeStorageSync('release2')
    wx.removeStorageSync('Date')
    wx.removeStorageSync('week')
  },
})